﻿using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;

namespace CodeCounsel.SharePoint.RibbonDesigner.Model
{
    [XName("Tab", Namespaces.CommandUIText)]
    public class Tab
        : ChildCollectionDomainElement<GroupContainer, Group>, ISequential
    {
        public int Sequence
        {
            get { return DataModel.GetAttributeValue<int>(Schema.Sequence); }
            set { DataModel.SetAttributeValue<int>(Schema.Sequence, value); }
        }

        public string Command
        {
            get { return DataModel.GetAttributeValue(Schema.Command); }
            set { DataModel.SetAttributeValue(Schema.Command, value); }
        }

        public string Description
        {
            get { return DataModel.GetAttributeValue(Schema.Description); }
            set { DataModel.SetAttributeValue(Schema.Description, value); }
        }

        public string Title
        {
            get { return DataModel.GetAttributeValue(Schema.Title); }
            set { DataModel.SetAttributeValue(Schema.Title, value); }
        }

        public string CssClass
        {
            get { return DataModel.GetAttributeValue(Schema.CssClass); }
            set { DataModel.SetAttributeValue(Schema.CssClass, value); }
        }

        public Scaling Scaling
        {
            get { return Children.OfType<Scaling>().FirstOrDefault(); }
        }

        public Tab(XElement dataModel, DomainModelSource source)
            : base(dataModel, source)
        {
        }

        public Tab()
        {
            Children.Add(new Scaling());
            Children.Add(new GroupContainer());
        }

        protected override void RaiseChildDomainElementRemoved(Group item)
        {
            OnGroupRemoved(new DomainElementEventArgs<Group>(item));
        }

        protected override void RaiseDomainElementRemoved(DomainElement item)
        {
            if (item is Scaling)
            {
                OnScalingRemoved(new DomainElementEventArgs<Scaling>((Scaling)item));
            }
            else if (item is GroupContainer)
            {
                OnGroupContainerRemoved(new DomainElementEventArgs<GroupContainer>((GroupContainer)item));
            }
        }

        protected virtual void OnScalingRemoved(DomainElementEventArgs<Scaling> e)
        {
            if (ScalingRemoved != null)
            {
                ScalingRemoved(this, e);
            }
        }

        protected virtual void OnGroupContainerRemoved(DomainElementEventArgs<GroupContainer> e)
        {
            if (GroupContainerRemoved != null)
            {
                GroupContainerRemoved(this, e);
            }
        }

        protected virtual void OnGroupRemoved(DomainElementEventArgs<Group> e)
        {
            if (GroupRemoved != null)
            {
                GroupRemoved(this, e);
            }
        }

        public event EventHandler<DomainElementEventArgs<Scaling>> ScalingRemoved;
        public event EventHandler<DomainElementEventArgs<GroupContainer>> GroupContainerRemoved;
        public event EventHandler<DomainElementEventArgs<Group>> GroupRemoved;
    }
}
